package ferp.core.mode;

import ferp.core.card.Deck;
import ferp.core.game.Game;
import ferp.core.game.Input;
import ferp.core.game.Settings;
import ferp.core.player.Hand;
import ferp.core.player.Player;
import ferp.core.player.Profile;
import ferp.core.state.State;
import ferp.core.tutorial.Scenario;

/**
 * User: user
 * Date: 6/8/11 11:32 PM
 */
public class Dealing extends Mode
{
/*
  @Override
  public void initialize(Game g)
  {
    super.initialize(g);

    Deck deck = g.deck();

    // reset trick
    g.trick.reset();

    // reset players
    for (Player p : g.players())
      p.reset();

    // clear game standing/open mode
    g.type = Game.Type.UNKNOWN;

    // starting from the player next to the dealer
    g.dealer = g.player(1);
    g.starting.player = g.next(g.dealer);

    // send cards to the players
    g.player(0).hand.current.assign(-977270525);
    g.player(1).hand.current.assign(539865616);
    g.player(2).hand.current.assign(303187176);

    // setup talon
    g.talon[0] = Card.restore((byte)51);
    g.talon[1] = Card.restore((byte)4);


    // show drop
    Log.log.debug(Log.TAG, "drop: " + Hand.dump(12582912));

    // show moves
    int n = 0, tricks[] = {1083310080};
    for (int t : tricks)
      Log.log.debug(Log.TAG, "trick #" + (++n) + ": " + Trick.dump(t, Card.Suit.HEARTS));

    g.consequentPassingSets = 0;

    // store the initial cards
    for (Player p : g.players())
      p.hand.initial = p.hand.current.hash();

    // fire after deal event to show player cards
    g.listener().onAfterDeal(g);

    // can not switch to the bidding mode from here!
    // must wait till the next processing cycle
  }
*/

  @Override
  public int initialize(Game.Listener listener, Profile profile, Game g, Settings settings) throws Game.Error
  {
    int r = super.initialize(listener, profile, g, settings);

    // reset the game object
    g.reset();

    if (g.tutorial())
    {
      Scenario scenario = g.scenario();

      for (Player player : g.players())
        g.getHand(player.id()).initial = g.getHand(player.id()).current = scenario.hand(player.id());

      for (int i = 0; i < Game.TALON_SIZE; ++i)
        g.setTalon(i, scenario.talon(i));
    }
    else
    {
      // starting from the player next to the dealer
      Player player = g.starting();
      Deck deck = Deck.instance();

      // shuffle deck
      deck.shuffle();

      // send cards to the players
      for (int i = 0; i < Deck.SIZE - Game.TALON_SIZE; player = g.next(player))
        for (int j = 0; j < 2; ++j)
          g.getHand(player.id()).initial = g.getHand(player.id()).current = Hand.add(g.getHand(player.id()).current, deck.card(i++).hash);

      // remaining cards go to the talon
      for (int i = 0; i < Game.TALON_SIZE; ++i)
        g.setTalon(i, deck.card(Deck.SIZE - Game.TALON_SIZE + i).hash);
    }

    // save the actual game's snapshot for replay
    profile.setInitialGame();

    // fire after deal event to show player cards
    listener.onAfterDeal(g);

    // switch to 'chill' state here to prevent 'options' method invocation
    // from 'chill' method the game can be safely moved to Bidding mode to properly
    // handle both regular and tutorial mode
    g.set(State.chill);

    return r;
  }

  @Override
  public int options(Game.Listener listener, Game g, Settings settings, Input input) throws Game.Error {return State.Result.SCHEDULE;}

  @Override
  public int chill(Game.Listener listener, Game g, Settings settings)
  {
    g.set(Mode.bidding);

    return super.chill(listener, g, settings);
  }
}
